//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    list: [],
    list2: [],
    list3: [],
    list4: [],
    head_index: 100,
    status: 0,
    showModal: false,
    showModal2: false,
    orderId: '',
    wait_num: 0, //待接单
    rader_num: 0, //配送中
    start_num: 0, //开始
    start_num2: 0, //开始
    start_num3: 0, //开始
    start_num4: 0, //开始
    end_num: 20, //结束
    Recharge_money: 0,
  },
  getOrderNum: function () {
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetShopOrderStatusNumber',
      data: { shopId: app.globalData.shopId },
      method: 'POST',
      success: function (res) {
        that.setData({
          wait_num: res.data.unDealOrderNumber,
          rader_num: res.data.dealingOrderNumber,
        })
      }
    })
  },
  onLoad: function () {
    var that = this
    /**
     * cs登录
     */
    wx.login({
      success: function (res) {
        wx.setStorageSync("code", res.code)
        wx.getUserInfo({
          success: function (res) {
            app.globalData.userInfo = res.userInfo
            var code = wx.getStorageSync('code')
            if (code != '') {
              wx.request({
                url: app.globalData._url + 'MenuAPI/WeChatLogin/GetSessionBoss',
                data: { code: code, nickName: app.globalData.userInfo.nickName, shopId: app.globalData.shopId },
                method: 'POST',
                success: function (res) {
                  console.log('登录:', res)
                  app.globalData.openId = res.data.openid
                }
              })
            }
          }
        })
      }
    })
    that.getOrderNum()
    var start = parseInt(that.data.start_num)
    var end = parseInt(that.data.end_num)
    var index = that.data.head_index
    that.loadData(index, start, end)
    wx.setNavigationBarTitle({
      title: app.globalData.shopName,
    })
  },
  onShow: function () {
    var that = this
    that.getOrderNum()
    that.getlastmoney()
    var index = that.data.head_index
    var start = 0
    var end = parseInt(that.data.end_num)
    console.log('index:', index)
    if (index == 100) { that.setData({ list: [] }), that.loadDatas(index, start, end) }
    if (index == 2) { that.setData({ list2: [] }), that.loadData2(index, start, end) }
    if (index == 3) { that.setData({ list3: [] }), that.loadData3(index, start, end) }
    if (index == 7) { that.setData({ list4: [] }), that.loadData4(index, start, end) }
  },
  getlastmoney: function () {
    var that = this
    wx.showToast({
      title: '数据加载中...',
      icon: 'loading',
      mask: true
    })
    wx.request({
      url: app.globalData._url + 'MenuAPI/Shop/GetEntity',
      data: { id: app.globalData.shopId },
      method: 'POST',
      success: function (res) {
        // console.log('entity:', res)
        if (res.data.status == 1) {
          that.setData({
            LastMoney: res.data.value.LastMoney
          })
        }
      },
      complete: function (res) {
        wx.hideToast()
      }
    })
  },
  reflastmoney: function () {
    this.getlastmoney()
  },
  toggle: function (e) { //全部
    var self = this
    var index = 100
    var start = 0
    console.log('点击切换start', start, '缓存:', app.globalData.list1)
    var end = parseInt(self.data.end_num)
    self.setData({
      list: [],
      status: index,
      head_index: index,
      start_num2: 0,
      start_num3: 0,
      start_num4: 0,
    })
    self.loadDatas(index, start, end)
    // this.setData({
    //   head_index: 100,
    //   list: app.globalData.list1
    // })
  },
  toggle2: function (e) { //待接单
    var self = this
    var index = 2
    var start = 0
    var end = parseInt(self.data.end_num)
    self.setData({
      list2: [],
      status: index,
      head_index: index,
      start_num: 0,
      start_num3: 0,
      start_num4: 0,
    })
    self.loadData2(index, start, end)
  },
  toggle3: function (e) { //配送中
    var self = this
    var start = 0
    var index = 3
    var end = parseInt(self.data.end_num)
    self.setData({
      list3: [],
      status: index,
      head_index: index,
      start_num: 0,
      start_num2: 0,
      start_num4: 0,
    })
    self.loadData3(index, start, end)
  },
  toggle4: function (e) { //已完成
    var self = this
    var index = 7
    var start = 0
    var end = parseInt(self.data.end_num)
    self.setData({
      list4: [],
      status: index,
      head_index: index,
      start_num: 0,
      start_num2: 0,
      start_num3: 0,
    })
    self.loadData4(index, start, end)
  },
  loadData: function (index, start, end) {
    wx.showLoading({
      title: '数据加载中...',
      mask: true,
    })
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetShopOrderList',
      data: { shopId: app.globalData.shopId, orderStatus: index, start: start, 'number': end },
      method: 'post',
      success: function (res) {
        var list = res.data.value
        for (var i = 0; i < list.length; i++) {
          if (list[i].OrderStatus == 2) {
            app.globalData.list2.push(list[i])
          } else if (list[i].OrderStatus == 3) {
            app.globalData.list3.push(list[i])
          } else if (list[i].OrderStatus == 7) {
            app.globalData.list4.push(list[i])
          }
        }
        app.globalData.list1 = res.data.value
        console.log('global1:', app.globalData.list1)
        // console.log('global2:', app.globalData.list2)
        // console.log('global3:', app.globalData.list3)
        // console.log('global4:', app.globalData.list4)
        if (res.data.status == 1) {
          var list = that.data.list
          for (var i = 0; i < res.data.value.length; i++) {
            list.push(res.data.value[i])
          }
          that.setData({
            list: list,
          })
        } else if (res.data.status == 0) {
          that.setData({
            list: []
          })
        }
      },
      fail: function (err) {
        console.log(err)
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  loadDatas: function (index, start, end) {
    wx.showLoading({
      title: '数据加载中...',
      mask: true,
    })
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetShopOrderList',
      data: { shopId: app.globalData.shopId, orderStatus: index, start: start, 'number': end },
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          var list = that.data.list
          if (res.data.value.length > 0) {
            for (var i = 0; i < res.data.value.length; ++i) {
              list.push(res.data.value[i])
            }
          }
          that.setData({
            list: list,
          })
        } else if (res.data.status == 0) {
          that.setData({
            list: []
          })
        }
      },
      fail: function (err) {
        console.log(err)
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  loadData2: function (index, start, end) {
    wx.showLoading({
      title: '数据加载中...',
      mask: true,
    })
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetShopOrderList',
      data: { shopId: app.globalData.shopId, orderStatus: index, start: start, 'number': end },
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          var list = that.data.list2
          if (res.data.value.length > 0) {
            for (var i = 0; i < res.data.value.length; ++i) {
              list.push(res.data.value[i])
            }
            that.setData({
              list2: list,
            })
          }
          if (res.data.value.length < list.length) {
            that.setData({
              list2: res.data.value,
            })
          }
        }
        else if (res.data.status == 0) {
          that.setData({
            list2: []
          })
        }
      },
      fail: function (err) {
        console.log(err)
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  loadData3: function (index, start, end) {
    wx.showLoading({
      title: '数据加载中...',
      mask: true,
    })
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetShopOrderList',
      data: { shopId: app.globalData.shopId, orderStatus: index, start: start, 'number': end },
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          var list = that.data.list3
          if (res.data.value.length > 0) {
            for (var i = 0; i < res.data.value.length; ++i) {
              list.push(res.data.value[i])
            }
            that.setData({
              list3: list,
            })
          }

        }
        else if (res.data.status == 0) {
          that.setData({
            list3: []
          })
        }
      },
      fail: function (err) {
        console.log(err)
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  loadData4: function (index, start, end) {
    wx.showLoading({
      title: '数据加载中...',
      mask: true,
    })
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetShopOrderList',
      data: { shopId: app.globalData.shopId, orderStatus: index, start: start, 'number': end },
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          var list = that.data.list4
          if (res.data.value.length > 0) {
            for (var i = 0; i < res.data.value.length; ++i) {
              list.push(res.data.value[i])
            }
          }
          that.setData({
            list4: list,
          })
        } else if (res.data.status == 0) {
          that.setData({
            list4: []
          })
        }
      },
      fail: function (err) {
        console.log(err)
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  hideModal: function () {
    this.setData({
      showModal2: false,
      showModal: false,
    });
  }, onCancel: function () {
    this.hideModal();
  }, onConfirm: function () {
    this.hideModal();
  },
  onHide: function () {
    this.setData({
      showModal: false,
    })
  },
  input: function (e) {
    this.setData({
      Recharge_money: e.detail.value
    })
  },
  toshowaddmy: function () {
    this.setData({
      showModal2: true
    })
  },
  //充值 
  addmoney: function () {
    var that = this
    var money = parseInt(that.data.Recharge_money)
    console.log("openid",app.globalData.openId, "shopid",app.globalData.shopId, money)
    wx.request({
      url: app.globalData._url + 'MenuAPI/Shop/Recharge',
      data: { openId: app.globalData.openId, shopId: app.globalData.shopId, money: money },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          var json_list = JSON.parse(res.data.value)
          wx.requestPayment({
            'timeStamp': json_list.timeStamp,
            'nonceStr': json_list.nonceStr,
            'package': json_list.package,
            'signType': json_list.signType,
            'paySign': json_list.paySign,
            success: function (res) {
              wx.showToast({
                title: '支付成功！',
                icon: 'success',
                duration: 1000,
              })
              that.getlastmoney()
              wx.hideToast()
              console.log(res)
            },
            complete: res => {
              that.setData({
                showModal2: false
              })
            }
          })
        }
      }
    })
  },
  updateOrderStatus: function (e) {
    var orderid = e.currentTarget.dataset.orderid
    console.log(orderid)
    this.setData({
      orderId: orderid,
      showModal: true
    })
  },
  toshoworderdel: function (e) {
    var orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: '../logs/logs?id=' + orderid,
    })
  },
  todetail: function (e) {
    var orderid = e.currentTarget.dataset.orderid
    var status = e.currentTarget.dataset.status
    var method = status == 'self' ? 1 : 2
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/UpdateShippingMethod',
      data: { orderId: orderid, method: method, orderStatus: 3 },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showLoading({
            title: '配送方式修改中',
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '../logs/logs?id=' + orderid + "&status=" + status,
            }), wx.hideLoading()
          }, 1000)

        } else if (res.data.status == -1) {
          wx.showModal({
            title: '提示',
            content: '余额不足，请及时充值',
            showCancel: false
          })
        }
      }
    })
  },
  toshowdetail: function (e) {
    var orderid = e.currentTarget.dataset.orderid
    var status = e.currentTarget.dataset.status
    console.log('传过去的status:', status)
    wx.navigateTo({
      url: '../logs/logs?id=' + orderid + "&status=" + status,
    })
  },
  onReachBottom: function () {
    var that = this
    var end = parseInt(that.data.end_num)
    var index = that.data.head_index
    if (index == 100) {
      that.setData({
        start_num: that.data.start_num + that.data.end_num,
      })
      var start_num = that.data.start_num + that.data.end_num
      var start = parseInt(that.data.start_num)
      that.loadDatas(index, start, end);
    }
    if (index == 2) {
      that.setData({
        start_num2: that.data.start_num2 + that.data.end_num,
      })
      var start = parseInt(that.data.start_num2)
      that.loadData2(index, start, end);
    }
    if (index == 3) {
      that.setData({
        start_num3: that.data.start_num3 + that.data.end_num,
      })
      var start = parseInt(that.data.start_num3)
      that.loadData3(index, start, end);
    }
    if (index == 7) {
      that.setData({
        start_num4: that.data.start_num4 + that.data.end_num,
      })
      var start = parseInt(that.data.start_num4)
      that.loadData4(index, start, end);
    }
  }
})
